(function($) {
    jQuery.fn.slidebox = function(options) {
        var defaults = {
            pagePercentage: 0.9
        };

        var options = $.extend(defaults, options);
        
        var slidebox = this;
        var originalPosition = slidebox.css('right');
        var open = false;
        $(window).scroll(function() {
            if ($(window).scrollTop() > 0 && $(window).height() + $(window).scrollTop() > options.pagePercentage * $(document).height()) {
                if (!open) {
                    open = true;
                    slidebox.animate({ 'right': '0px' }, 300);
                }
            } else {
                open = false;
                slidebox.stop(true).animate({ 'right': originalPosition }, 100);
            }
        });
        slidebox.find('.slidebox-close').click(function(event) {
            event.preventDefault();
            slidebox.fadeOut(function() { 100, slidebox.remove(); });
        });
    }
})(jQuery);